package com.tanjun.userservice.web.biz.admin;

import com.mybatisflex.core.query.QueryChain;
import com.tanjun.common.enums.menu.MenuTypeEnum;
import com.tanjun.common.utils.ConvertUntil;
import com.tanjun.userservice.apiservice.request.menu.AdminMenuRequest;
import com.tanjun.userservice.apiservice.response.adminmenu.admin.AdminMenuResponse;
import com.tanjun.userservice.apiservice.response.adminmenu.admin.AdminMenuRouteResponse;
import com.tanjun.userservice.apiservice.response.adminmenu.admin.AdminMenuTreeResponse;
import com.tanjun.userservice.web.convert.AdminMenuConvert;
import com.tanjun.userservice.web.service.menu.entity.BaseAdminMenuEntity;
import com.tanjun.userservice.web.service.menu.entity.BaseAdminRoleMenuEntity;
import com.tanjun.userservice.web.service.menu.service.BaseAdminMenuService;
import com.tanjun.web.service.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class AdminMenuBiz {
    @Autowired
    private BaseAdminMenuService adminMenuService;
//    @Autowired
//    private BaseAdminRoleMenuService baseAdminRoleMenuService;
    /**
     * 新增用户菜单
     *
     * @param menu 菜单
     */
 
    public void addMenu(AdminMenuRequest menu) {

        BaseAdminMenuEntity adminMenu= ConvertUntil.convert(menu, BaseAdminMenuEntity.class);
        adminMenu.setCreator(LoginUserUtil.getAdminOperate());
        adminMenuService.saveMenu(adminMenu);
    }

    /**
     * 获取菜单树形结构
     *
     * @return 返回所有的菜单
     */
 
    public List<AdminMenuTreeResponse> listTree() {
        List<BaseAdminMenuEntity> list= adminMenuService.list();
        List<BaseAdminMenuEntity> listCatalog=list.stream()
                .filter(t->t.getParentId()==null||t.getParentId()==0)
                .collect(Collectors.toList());
        List<AdminMenuTreeResponse> listDto=ConvertUntil.convert(listCatalog, AdminMenuTreeResponse.class);
        listDto.forEach(d->{
            getChildren(d,list,true);
        });
        return listDto;
    }

    /**
     * 获取树形结构(不包括按钮)
     * @param includeButton  是否包含按钮
     * @return 树形结构
     */
 
    public List<AdminMenuTreeResponse> tree(Boolean includeButton) {
        List<BaseAdminMenuEntity> list= adminMenuService.list();
        List<BaseAdminMenuEntity> listCatalog=list.stream()
                .filter(t->t.getParentId()==null||t.getParentId()==0)
                .collect(Collectors.toList());
        List<AdminMenuTreeResponse> listDto=ConvertUntil.convert(listCatalog, AdminMenuTreeResponse.class);
        listDto.forEach(d->{
            getChildren(d,list,includeButton);
        });
        return listDto;
    }

    /**
     * 更新菜单
     *
     * @param menu 菜单数据
     */
 
    public void updateMenu(AdminMenuRequest menu) {
        BaseAdminMenuEntity adminMenu= ConvertUntil.convert(menu, BaseAdminMenuEntity.class);
        adminMenu.setUpdater(LoginUserUtil.getAdminOperate());
        adminMenuService.saveMenu(adminMenu);
    }

    /**
     * 获取菜单有关基础数据
     *
     * @param menuId 菜单id
     * @return 菜单数据
     */
 
    public AdminMenuResponse getMenuById(Long menuId) {
        BaseAdminMenuEntity menu= adminMenuService.getById(menuId);
        return ConvertUntil.convert(menu, AdminMenuResponse.class);
    }

    /**
     * 删除菜单数据
     *
     * @param menuId 菜单id
     */
 
    public void delMenuById(Long menuId) {
        adminMenuService.delMenu(menuId);
    }



    /**
     * 获取路由对象
     *
     * @return
     */
 
    public List<AdminMenuRouteResponse> getRoutes() {
        List<BaseAdminMenuEntity> listAll= adminMenuService.list();
        List<BaseAdminMenuEntity> listCatalog=listAll.stream()
                .filter(t->t.getParentId()==null||t.getParentId()==0)
                .sorted(Comparator.comparingInt(BaseAdminMenuEntity::getSort))
                .toList();
        List<AdminMenuRouteResponse> list= AdminMenuConvert.convertToRoutes(listCatalog);
        list.forEach(t->{
            getChildrenRoutes(t,listAll);
        });
        return list;
    }

    /**
     * 获取所有的子树形结构
     * @param dto 需要获取的节点
     * @param listAll 所有的数据
     */
    private void getChildren(AdminMenuTreeResponse dto, List<BaseAdminMenuEntity> listAll, boolean includeButton){
        List<BaseAdminMenuEntity> listChildren=listAll.stream()
                .filter(t->t.getParentId().equals(dto.getMenuId()))
                .filter(t-> includeButton || t.getType() != MenuTypeEnum.BUTTON)
                .collect(Collectors.toList());
        if(listChildren.isEmpty() ){
            return;
        }
        dto.setChildren(ConvertUntil.convert(listChildren, AdminMenuTreeResponse.class));
        dto.getChildren().forEach(d->{
            getChildren(d,listAll,includeButton);
        });
    }


    /**
     * 获取所有的子树形结构
     * @param dto 需要获取的节点
     * @param listAll 所有的数据
     */
    private void getChildrenRoutes(AdminMenuRouteResponse dto, List<BaseAdminMenuEntity> listAll){
        List<BaseAdminMenuEntity> listChildren=listAll.stream()
                .filter(t->t.getParentId().equals(dto.getMenuId()))
                .filter(t->t.getType() != MenuTypeEnum.BUTTON)
                .collect(Collectors.toList());
        if(listChildren.isEmpty() ){
            return;
        }
        dto.setChildren(AdminMenuConvert.convertToRoutes(listChildren));
        dto.getChildren().forEach(d->{
            getChildrenRoutes(d,listAll);
        });
    }

}

